package com.ccn.ui.a2screenadaptation;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.ccn.ui.R;

public class A2_1_2PercentLayout extends RelativeLayout {
    public A2_1_2PercentLayout(Context context) {
        this(context, null);
    }

    public A2_1_2PercentLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public A2_1_2PercentLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //获取父容器尺寸
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            View childAt = getChildAt(i);
            ViewGroup.LayoutParams layoutParams = childAt.getLayoutParams();
            //检查是否为PercentLayout的LayoutParams
            if (checkLayoutParams(layoutParams)) {
                LayoutParams lp = (LayoutParams) layoutParams;
                //宽度百分比
                if (lp.widthPercent > 0) {
                    lp.width = (int) (widthSize * lp.widthPercent);
                }
                //高度百分比
                if (lp.heightPercent > 0) {
                    lp.height = (int) (heightSize * lp.heightPercent);
                }
                //左侧间距百分比
                if (lp.marginLeftPercent > 0) {
                    lp.leftMargin = (int) (widthSize * lp.marginLeftPercent);
                }
                //右侧间距百分比
                if (lp.marginRightPercent > 0) {
                    lp.rightMargin = (int) (widthSize * lp.marginRightPercent);
                }
                //顶部间距百分比
                if (lp.marginTopPercent > 0) {
                    lp.topMargin = (int) (heightSize * lp.marginTopPercent);
                }
                //底部间距百分比
                if (lp.marginBottomPercent > 0) {
                    lp.bottomMargin = (int) (heightSize * lp.marginBottomPercent);
                }
            }
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
        return p instanceof LayoutParams;
    }

    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new LayoutParams(getContext(), attrs);
    }

    public static class LayoutParams extends RelativeLayout.LayoutParams {

        private float widthPercent;
        private float heightPercent;
        private float marginLeftPercent;
        private float marginRightPercent;
        private float marginTopPercent;
        private float marginBottomPercent;

        public LayoutParams(Context c, AttributeSet attrs) {
            super(c, attrs);
            TypedArray a = c.obtainStyledAttributes(attrs, R.styleable.A2_1_2PercentLayout);
            widthPercent = a.getFloat(R.styleable.A2_1_2PercentLayout_widthPercent, 0);
            heightPercent = a.getFloat(R.styleable.A2_1_2PercentLayout_heightPercent, 0);
            marginLeftPercent = a.getFloat(R.styleable.A2_1_2PercentLayout_marginLeftPercent, 0);
            marginRightPercent = a.getFloat(R.styleable.A2_1_2PercentLayout_marginRightPercent, 0);
            marginTopPercent = a.getFloat(R.styleable.A2_1_2PercentLayout_marginTopPercent, 0);
            marginBottomPercent = a.getFloat(R.styleable.A2_1_2PercentLayout_marginBottomPercent, 0);
            a.recycle();
        }
    }
}
